﻿// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone

using Longbow.Logging;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// 邮件日志扩展方法
/// </summary>
static class CloudLoggerExtensions
{
    /// <summary>
    /// 注册邮件日志方法
    /// </summary>
    /// <param name="builder"></param>
    /// <returns></returns>
    public static ILoggingBuilder AddCloudLogger(this ILoggingBuilder builder)
    {
        builder.Services.AddSingleton<IConfigureOptions<CloudLoggerOption>, LoggerProviderConfigureOptions<CloudLoggerOption, CloudLoggerProvider>>();
        builder.Services.AddSingleton<IOptionsChangeTokenSource<CloudLoggerOption>, LoggerProviderOptionsChangeTokenSource<CloudLoggerOption, CloudLoggerProvider>>();
        builder.Services.AddSingleton<ILoggerProvider, CloudLoggerProvider>();
        return builder;
    }
}

/// <summary>
/// 云日志提供类
/// </summary>
[ProviderAlias("Cloud")]
class CloudLoggerProvider : LoggerProvider
{
    private readonly HttpClient httpClient;
    private readonly IDisposable optionsReloadToken;
    private CloudLoggerOption option;

    /// <summary>
    /// 构造函数
    /// </summary>
    public CloudLoggerProvider(IOptionsMonitor<CloudLoggerOption> options) : base(null, new Func<string, LogLevel, bool>((name, logLevel) => logLevel >= LogLevel.Error))
    {
        optionsReloadToken = options.OnChange(op => option = op);
        option = options.CurrentValue;

        httpClient = new HttpClient
        {
            Timeout = TimeSpan.FromSeconds(10)
        };
        httpClient.DefaultRequestHeaders.Connection.Add("keep-alive");

        LogCallback = new Action<string>(async message =>
        {
            if (!string.IsNullOrEmpty(option.Url))
            {
                try { await httpClient.PostAsJsonAsync(option.Url, message); }
                catch { }
            }
        });
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="disposing"></param>
    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        if (disposing)
        {
            httpClient.Dispose();
            optionsReloadToken.Dispose();
        }
    }
}

/// <summary>
/// 云日志配置类
/// </summary>
class CloudLoggerOption
{
    /// <summary>
    /// 获得/设置 云日志地址
    /// </summary>
    public string Url { get; set; } = "";
}
